package com.qire.manhua.dialog;

import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.qire.common.basic.ObjectUtil;
import com.qire.common.basic.StringUtil;
import com.qire.common.support.base.BaseDialog;
import com.qire.common.utils.OSUtil;
import com.qire.common.utils.ThemeUtil;
import com.qire.common.utils.ToastHelper;
import com.qire.common.widget.adapter.auto.SummerBindAdapter;
import com.qire.manhua.BR;
import com.qire.manhua.R;
import com.qire.manhua.controller.ShareGridController;
import com.qire.manhua.dal.service.TaskReportService;
import com.qire.manhua.databinding.DialogShareComicBinding;
import com.qire.manhua.model.entity.ShareInfo;
import com.qire.manhua.model.viewModel.impl.ShareItemViewModel;
import com.qire.other.share.ShareActionFactory;

import java.util.HashMap;

import androidx.annotation.NonNull;
import androidx.databinding.ViewDataBinding;

/**
 * 分享漫画对话框：在漫画详情页，漫画阅读页等页面使用显示
 */
public class ShareComicDialog extends BaseDialog implements ShareGridController {

    public final SummerBindAdapter shareAdapter = new SummerBindAdapter(ShareItemViewModel.buildToShareComic(), this::bindVariable);

    private final DialogShareComicBinding viewDataBinder;

    private final ShareInfo shareInfo;

    public ShareComicDialog(@NonNull Context context, ShareInfo shareInfo) {
        super(context, R.style.BottomDialog);
        viewDataBinder = DialogShareComicBinding.inflate(LayoutInflater.from(context));
        viewDataBinder.setShareComicDialog(this);
        setContentView(viewDataBinder.getRoot());
        this.shareInfo = shareInfo;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ThemeUtil.setWindowGravityBottom(getWindow(), ViewGroup.LayoutParams.WRAP_CONTENT);
    }

    public void bindVariable(ViewDataBinding viewBind) {
        viewBind.setVariable(BR.shareGridController, this);
    }

    public void closeBtnOnClick(View view) {
        dismiss();
    }

    @Override
    public void shareItemOnClick(View view) {
        ShareItemViewModel shareItemViewModel = ObjectUtil.as(view.getTag(), ShareItemViewModel.class);
        if(ObjectUtil.isNull(shareItemViewModel) || ObjectUtil.isNull(shareInfo)) {
            ToastHelper.showToast("分享失败：未获得分享数据！");
        } else {
            switch (shareItemViewModel.shareType) {
                case WECHATMOMENTS:
                case WECHAT:
                case PLATFORM_QQ:
                case PLATFORM_QZONE:
                case SINAWEIBO:
                    thirdPartyShare(shareItemViewModel);
                    break;
                case COPY_LINKS :
                    String text = shareInfo.urlPage;
                    OSUtil.copyToClipboard(view.getContext(), "link", text);
                    ToastHelper.showToast("链接复制成功");
                    break;
            }
        }
        dismiss();
    }

    private void thirdPartyShare(ShareItemViewModel shareItemViewModel) {
        HashMap<String, Object> shareMap = new HashMap<>();
        // todo： 需要处理分享效果问题
        shareMap.put("shareType", 4);//Platform.SHARE_WEBPAGE);
        shareMap.put("title", shareInfo.title);//测试分享的标题
        shareMap.put("text", shareInfo.content);//测试分享的文本
        shareMap.put("url", shareInfo.urlPage);// 发布分享网站的地址
        shareMap.put("imageUrl", StringUtil.appendTimestampTo(shareInfo.urlImage));//测试分享的图片

        shareMap.put("site", shareInfo.title);//发布分享的网站名称
        shareMap.put("siteUrl", shareInfo.urlPage);//发布分享网站的地址
        shareMap.put("titleUrl", shareInfo.urlPage);// 标题的超链接

        ShareActionFactory.create(shareItemViewModel.shareType.shareName)
                .setShareOnErrorListener(this::shareFail)
                .setShareOnCancelListener(this::shareFail)
                .setShareOnCompleteListener(this::shareSuccess)
                .doShareAction(shareMap);
    }

    private void shareFail() {
        ToastHelper.showToast("分享失败");
    }

    private void shareSuccess() {
        ToastHelper.showToast("分享成功");
        new TaskReportService().shareComicTaskReport();
    }

}
